<?php

namespace app\admin\controller;

use app\admin\model\Membertype as MembertypeMdl;
use app\admin\model\Memberdefaultimage;
use app\common\validate\MemberType as validateMemberType;
use app\admin\model\Product;

class Membertype extends Base
{

    private function getTenantMemberType(){
        $tenantId = session("TENANT_ID");
        $memberTypeModel = new \app\admin\model\Membertype();
        return $memberTypeModel->getTemantMemberTypeList($tenantId);
    }

    public function index(){
        //获取当前商户会员类型
        $tenantMemberTypeList = self::getTenantMemberType();
        $this->assign("memberList",$tenantMemberTypeList);
        return $this->fetch();
    }

    private function getMemberDefultImageList(){
        $memberdefaultimageModel = new Memberdefaultimage();
        //$list = $memberdefaultimageModel->getMemberdefaultimageList();
        $where = "`type` = 1 OR (`type` = 2 AND `tenantId` = '" . session('TENANT_ID') . "')";
        $list = $memberdefaultimageModel->where($where)->select();

        return $list;
    }

    public function add(){
        $tenantId = session("TENANT_ID");
        if(request()->isPost()){
            if(input('post.isMemBerShop') == '0' && !$this->checkPrice()){
                return show(config('status.ERROR_STATUS'),'有商品会员价为0，请修改','有商品会员价为0，请修改');
            }
            //检测数据合法性
            (new validateMemberType())->goCheck();
            $Membertype = new \app\admin\model\Membertype();
            if($Membertype->addmembertype(input('post.'))){
                return show(config('status.SUCCESS_STATUS'),'ok','ok');
            }else{
                return show(config('status.ERROR_STATUS'),'error','error');
            }
        }else{
            //获取商户会员卡默认图片id
            $memberDefultImageList = $this->getMemberDefultImageList();

            $this->assign("memberDefultImageList",$memberDefultImageList);
            $this->assign('TENANT_ID',$tenantId);
            return $this->fetch();
        }

    }

    public function edit(){
        $tenantId = session("TENANT_ID");
        if(request()->isPost()){
            if(input('post.isMemBerShop') == '0'  && !$this->checkPrice()){
                return show(config('status.ERROR_STATUS'),'有商品会员价为0，请修改','有商品会员价为0，请修改');
            }
            //检测数据合法性
            (new validateMemberType())->goCheck();
            $Membertype = new \app\admin\model\Membertype();
            if($Membertype->editMembertype(input('post.'))){
                return show(config('status.SUCCESS_STATUS'),'ok','ok');
            }else{
                return show(config('status.ERROR_STATUS'),'error','error');
            }
        }else{
            $id = $this->request->param('id');
            $member = MembertypeMdl::where(['id' => $id])->find();
            //获取商户会员卡默认图片id
            $memberDefultImageList = $this->getMemberDefultImageList();

            $this->assign('info', $member);
            $this->assign("memberDefultImageList",$memberDefultImageList);
            $this->assign('TENANT_ID',$tenantId);

            return $this->fetch();
        }

    }

    public function checkPrice() {  //检测memberPrice是否填写
        $find = Product::where(['tenantId'=>session('TENANT_ID'),'memberPrice'=>0])->find();
        if($find) {
            return false;
        }
        return true;
    }

    /**
     * 删除
     */
    public function delete(){
        if($this->request->isAjax()){
            $id = $this->request->param('id');
            if(!$id){
                return json(['status' => 0,'msg' => '参数错误！']);
            }

            $result = MembertypeMdl::where(['id' => $id])->delete();
            if($result){
                return json(['status' => 1,'msg' => '删除成功！']);
            }else{
                return json(['status' => 0,'msg' => '删除失败！']);
            }
        }
    }

    /**
     * 启用
     */
    public function enable(){
        if($this->request->isAjax()){
            $id = $this->request->param('id');
            if(!$id){
                return json(['status' => 0,'msg' => '参数错误！']);
            }

            $result = MembertypeMdl::where(['id' => $id])->update(['status' => 1]);
            if($result){
                return json(['status' => 1,'msg' => '启用成功！']);
            }else{
                return json(['status' => 0,'msg' => '启用失败！']);
            }
        }
    }

    /**
     * 禁用
     */
    public function disable(){
        if($this->request->isAjax()){
            $id = $this->request->param('id');
            if(!$id){
                return json(['status' => 0,'msg' => '参数错误！']);
            }

            $result = MembertypeMdl::where(['id' => $id])->update(['status' => 2]);
            if($result){
                return json(['status' => 1,'msg' => '禁用成功！']);
            }else{
                return json(['status' => 0,'msg' => '禁用失败！']);
            }
        }
    }
}